package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    //新增套餐
    @Override
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //插入套餐
        setmealMapper.insert(setmeal);

        Long setmealId = setmeal.getId();
        //套餐中插入多个菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0){
            setmealDishMapper.insertBatch(setmealDishes,setmealId);
        }
    }

    //套餐分页查询
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealDishMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //批量删除套餐
    @Override
    public void deleteByIds(List<Long> ids) {
        //判断套餐是否能删除--状态是否为起售中
        Long num = setmealMapper.getStatus(ids, StatusConstant.ENABLE);
        if(num > 0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }

//        setmealMapper.deleteByIds(ids);
        for(Long id : ids){
            //根据id删除套餐
            setmealMapper.deleteById(id);
            //根据id删除套餐中的菜品
            setmealDishMapper.deleteById(id);
        }
    }

    //根据id返回套餐
    @Override
    public SetmealVO selectById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setmealMapper.selectById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        //根据id返回套餐中的菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.selectById(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }
    //修改套餐
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //删除原套餐中的菜品，重新插入
        Long setmealId = setmeal.getId();
        setmealDishMapper.deleteById(setmealId);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishMapper.insertBatch(setmealDishes,setmealId);
    }

    //起售停售套餐
    @Override
    public void startOrStop(Integer status, Long id) {//setmeal中的id，即sermeal_dish中的sermealId
        //判断套餐中是否含未启售的菜品
        Long num = setmealDishMapper.selectStatus(id,StatusConstant.DISABLE);//查询菜品状态为停售的个数
        if(num > 0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
        }

        Setmeal setmeal = Setmeal.builder()//注意这里的格式  类型.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);
    }

    //客户端
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
